package com.danan.realtime.app.dwd;

import com.danan.realtime.app.BaseSqlApp;
import com.danan.realtime.common.Constant;
import com.danan.realtime.util.SinkUtil;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @author NanHuang
 * @Date 2023/1/25
 */
public class DwdApp_7_trade_order_refund_success_detail extends BaseSqlApp {
    public static void main(String[] args) {
        new DwdApp_7_trade_order_refund_success_detail().init(4007,3,"DwdApp_7_trade_order_refund_success_detail");
    }
    @Override
    public void invoke(StreamExecutionEnvironment env, StreamTableEnvironment tableEnv) {
        // 1 读取ods_db数据
        readOdsDb(tableEnv,"DwdApp_7_trade_order_refund_success_detail");
        // 2 过滤退款成功数据
        filterRefundSucInfo(tableEnv);
        // 3 过滤退单表中的数据
        filterRefundInfo(tableEnv);
        // 4 过滤订单表中的数据
        filterOrderInfo(tableEnv);
        // 5 读取字典表
        readBaseDic(tableEnv);
        // 6 join
        join(tableEnv);
        // 7 将数据写入到Kafka
        writeToKafka(tableEnv);
    }

    private void writeToKafka(StreamTableEnvironment tableEnv) {
        tableEnv.executeSql("create table target (" +
                "id string," +
                "user_id string," +
                "order_id string," +
                "sku_id string," +
                "province_id string," +
                "payment_type_code string," +
                "payment_type_name string," +
                "date_id string," +
                "callback_time string," +
                "refund_num string," +
                "refund_amount string," +
                "ts bigint " +
                ")" +
                SinkUtil.getKafkaWith(Constant.TOPIC_DWD_TRADE_REFUND_PAY_SUC));
        tableEnv.executeSql("insert into target select * from join_result");
    }

    private void join(StreamTableEnvironment tableEnv) {
        Table joinResult = tableEnv.sqlQuery("select " +
                "rpi.id," +
                "oi.user_id," +
                "rpi.order_id," +
                "rpi.sku_id," +
                "oi.province_id," +
                "rpi.payment_type," +
                "dic.dic_name payment_type_name," +
                "date_format(rpi.callback_time,'yyyy-MM-dd') date_id," +
                "rpi.callback_time," +
                "ori.refund_num," +
                "rpi.total_amount," +
                "rpi.ts " +
                "from refund_suc_info rpi " +
                "join refund_info ori on rpi.order_id = ori.order_id " +
                "join order_info oi on rpi.order_id = oi.id " +
                "join base_dic for system_time as of rpi.pt as dic on rpi.payment_type = dic.dic_code");
        tableEnv.createTemporaryView("join_result",joinResult);
    }

    private void filterOrderInfo(StreamTableEnvironment tableEnv) {
        Table orderInfo = tableEnv.sqlQuery("select " +
                "data['id'] id," +
                "data['user_id'] user_id," +
                "data['province_id'] province_id," +
                "`old` " +
                "from source " +
                "where `database`='gmall2023' and `table`='order_info' " +
                "and `old`['order_status'] is not null " +
                "and `data`['order_status']='1006'");
        tableEnv.createTemporaryView("order_info",orderInfo);
    }

    private void filterRefundInfo(StreamTableEnvironment tableEnv) {
        Table refundInfo = tableEnv.sqlQuery("select " +
                "data['order_id'] order_id," +
                "data['sku_id'] sku_id," +
                "data['refund_num'] refund_num " +
                "from source " +
                "where `database`='gmall2023' and `table`='order_refund_info' " +
                "and `type`='insert'");
        tableEnv.createTemporaryView("refund_info",refundInfo);
    }

    private void filterRefundSucInfo(StreamTableEnvironment tableEnv) {
        Table refundSucInfo = tableEnv.sqlQuery("select " +
                "data['id'] id," +
                "data['order_id'] order_id," +
                "data['sku_id'] sku_id," +
                "data['payment_type'] payment_type," +
                "data['callback_time'] callback_time," +
                "data['total_amount'] total_amount," +
                "pt," +
                "ts " +
                "from source " +
                "where `database`='gmall2023' and `table`='refund_payment' " +
                "and `type`='insert'");
        tableEnv.createTemporaryView("refund_suc_info",refundSucInfo);
    }
}
